import { defineView } from "@/routers/tools.mjs";

/**
 * @type {DefineView}
 */
const NgResourceView = defineView({
  controller: class {
    /**
     * @param {ng.resource.IResourceService} $resource
     */
    constructor($resource) {
      this.$resource = $resource;
    }

    get() {
      // const t = /\t\s/i;
      // Define a CreditCard class
      const CreditCard = this.$resource(
        "/users/:userId/cards/:cardId",
        { userId: 123, cardId: "@id" },
        {
          charge: { method: "POST", params: { charge: true } },
        },
      );

      // We can retrieve a collection from the server
      const cards = CreditCard.query();
      // GET: /users/123/cards
      // server returns: [{id: 456, number: '1234', name: 'Smith'}]

      // Wait for the request to complete
      // cards.$promise.then(function () {
      //   var card = cards[0];

      //   // Each item is an instance of CreditCard
      //   expect(card instanceof CreditCard).toEqual(true);

      //   // Non-GET methods are mapped onto the instances
      //   card.name = "J. Smith";
      //   card.$save();
      //   // POST: /users/123/cards/456 {id: 456, number: '1234', name: 'J. Smith'}
      //   // server returns: {id: 456, number: '1234', name: 'J. Smith'}

      //   // Our custom method is mapped as well (since it uses POST)
      //   card.$charge({ amount: 9.99 });
      //   // POST: /users/123/cards/456?amount=9.99&charge=true {id: 456, number: '1234', name: 'J. Smith'}
      // });

      // We can create an instance as well
      // var newCard = new CreditCard({ number: "0123" });
      // newCard.name = "Mike Smith";

      // var savePromise = newCard.$save();
      // // POST: /users/123/cards {number: '0123', name: 'Mike Smith'}
      // // server returns: {id: 789, number: '0123', name: 'Mike Smith'}

      // savePromise.then(function () {
      //   // Once the promise is resolved, the created instance
      //   // is populated with the data returned by the server
      //   expect(newCard.id).toEqual(789);
      // });
    }
  },
  template: /* HTML */ html` <!-- Target? --> `,
});

export default NgResourceView;
